import { configureStore } from '@reduxjs/toolkit';
import logger from 'redux-logger';
import { persistStore } from 'redux-persist';
import reducer from './reducer';

// 声明一个什么也不做的中间件，用于解决开发环境的报错
function emptyMiddleware() {
  return function (next: any) {
    return (action: any) => {
      return next(action);
    };
  };
}

const store = configureStore({
  reducer,
  middleware: (getDefaultMiddleware) => {
    return getDefaultMiddleware({
      serializableCheck: false,
    }).concat(!__PRODUCTION__ ? logger : emptyMiddleware);
  },
  devTools: !__PRODUCTION__,
});

export const persistor = persistStore(store);
export type AppDispatch = typeof store.dispatch;
export type RootState = ReturnType<typeof store.getState>;
export default store;
